package com.objectproject.main.service;

import java.util.ArrayList;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.objectproject.main.dao.MyObjectDAOImpl;
import com.objectproject.main.dao.MyPrimitiveDAOImpl;
import com.objectproject.main.dao.MyPropertyDAOImpl;
import com.objectproject.main.exception.ObjectHierarchyException;
import com.objectproject.main.exception.RenderFailException;
import com.objectproject.main.model.MyObject;
import com.objectproject.main.model.MyProperty;
import com.objectproject.main.service.iface.MyObjectService;
import com.objectproject.main.service.subsystem.MyLocaleResolver;
import com.objectproject.main.service.subsystem.MyObjectHierarchyResolver;
import com.objectproject.main.service.subsystem.PrimitiveRenderer;
import com.objectproject.main.service.subsystem.loader.PropertyDetailLoader;
import com.objectproject.main.service.subsystem.loader.PropertyLoader;
import com.objectproject.main.service.subsystem.loader.ValueLoader;
import com.objectproject.main.service.subsystem.utility.PropertyUtility;

@Service
public class MyObjectServiceImpl implements MyObjectService {

	@Autowired
	MyObjectDAOImpl myObjectDAOImpl;
	
	@Autowired
	MyPropertyDAOImpl myPropertyDAOImpl;
	
	@Autowired
	MyPrimitiveDAOImpl myPrimitiveDAOImpl;
	
	@Autowired
	MyLocaleResolver myLocaleResolver;
	
	@Autowired
	MyPropertyServiceImpl myPropertyServiceImpl;
	
	@Autowired
	MyObjectHierarchyServiceImpl myObjectHierarchyServiceImpl;
	
	@Autowired
	MyObjectHierarchyResolver myObjectHierarchyResolver;
	
	@Autowired
	PropertyUtility propertyUtility;
	
	@Autowired
	PropertyLoader propertyLoader;
	
	@Autowired
	PropertyDetailLoader propertyDetailLoader;
	
	@Autowired
	ValueLoader valueLoader;
	
	@Autowired
	PrimitiveRenderer primitiveRenderer;
	
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public long createMyObject(MyObject myObject, Locale locale) throws ObjectHierarchyException {
		
		// 1.객체 삽입
		myObjectDAOImpl.insertMyObject(myObject);

		// 2.계층구조 삽입(+깊이 설정)
		myObjectHierarchyServiceImpl.isA(myObject, myObject.getAncestorObjects());
		
		// 3.속성 삽입(계층구조 삽입과 순서 바뀌어도 괜찮음)
		myPropertyServiceImpl.addMyProperties(myObject, myObject.getMyProperties(), locale);
		
		// 4.관련 객체 삽입
		
		// 5.객체 식별자 리턴
		return myObject.getObjectId();
	}

	@Override
	public void removeMyObject(long objectId) {
		myObjectDAOImpl.protectedDeleteMyObjectById(objectId);
	}

	@Override
	public void reviseMyObject(MyObject myObject) {
		// TODO Auto-generated method stub
	}

	@Override
	public MyObject getMyObject(long objectId, Locale locale) throws RenderFailException {
		// 1. 유효한지 검사
		
		MyObject myObject = myObjectDAOImpl.selectMyObjectById(objectId);
		propertyLoader.loadHierarchicalFullPropertiesWithOverriding(myObject);
		propertyDetailLoader.loadPropertyDetails(myObject, myLocaleResolver.resolveLocaleId(locale));
		valueLoader.loadAllPropertyValuesOf(myObject, myLocaleResolver.resolveLocaleId(locale));
		primitiveRenderer.renderValues(myObject);
		return myObject;
	}

}
